﻿//Write a program that finds the most frequent number in an array. Example:
//	{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3}  4 (5 times)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _09MostFrequentNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Array size:");
            string strArraySize = Console.ReadLine();
            int arraySize = int.Parse(strArraySize);
            int[] array = new int[arraySize];
            for (int i = 0; i <= array.Length - 1; i++)
            {
                Console.Write("array[{0}]:", i);
                string strValue = Console.ReadLine();
                array[i] = int.Parse(strValue);
            }
            Console.WriteLine("The mixed array looks like this:");
            foreach (var item in array)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            for (int i = 0; i <= array.Length - 2; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[i] > array[j])
                    {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            Console.WriteLine("The sorted array looks like this:");
            foreach (var item in array)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            int bestSequence = 1;
            int currentSequence = 1;
            int bestElement = array[1];
            for (int i = 0; i < array.Length - 1; i++)
            {
                if (array[i] == array[i + 1])
                {
                    currentSequence++;
                }
                else
                {
                    currentSequence = 1;
                }
                if (currentSequence > bestSequence)
                {
                    bestSequence = currentSequence;
                    bestElement = array[i];
                }
            }
            Console.WriteLine("The longest sequence of equal elements is:");
            for (int i = 0; i < bestSequence; i++)
            {
                Console.Write(bestElement + " ");
            }
            Console.WriteLine();
            Console.WriteLine("The most repeated element is {0} and it appears {1} times.", bestElement, bestSequence);
        }
    }
}
